/*
 *  Startup.java
 *
 *  Course Project for User Interface Programming I, 2011
 *  Group 19
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 */
package justdoit;

import java.util.Locale;
import javax.swing.JOptionPane;
import justdoit.controller.TaskManager;
import justdoit.model.DefaultSettings;
import justdoit.model.User;
import justdoit.controller.LanguageManager;
import justdoit.controller.ReminderInitiator;
import justdoit.controller.XMLManager;
import justdoit.view.LanguageChooserDialog;
import justdoit.view.LoginDialog;
import justdoit.view.MainFrame;

/**
 * The Main entry for This Program.
 * Loads the language file and creates the main window.
 * @author Can
 */
public class Startup {

    private static MainFrame mainFrame;
    private static DefaultSettings ds;

    public static DefaultSettings getDs() {
        return ds;
    }

    public static MainFrame getMainFrame() {
        return mainFrame;
    }

    Startup() {
        //TODO: need to be improved
        XMLManager xm = new XMLManager();
        ds = xm.getDefaultSettings();
        if (ds == null) {
            System.exit(-1);
        }
        User currentUser = null;
        if (ds.getUserName() != null && ds.getUserName() != "") {
            currentUser = TaskManager.getUserByUserName(ds.getUserName());
        }
        // if no default user specified
        if (currentUser == null) {
            String[] names = TaskManager.getUserNameList();
            if ((names == null) || (names.length == 0)) {
                // no user exists
                // choose the language at first time
                LanguageChooserDialog lcd = new LanguageChooserDialog();
                lcd.setVisible(true);
                loadLanguage(ds.getLocal());
                System.out.println("No user exists! Create a default one!");
                currentUser = TaskManager.createDefaultUser(ds.getLocal());
                ds.setUserName(currentUser.getUserName());
            } else {
                // user exists
                for (int i = 0; i < names.length; i++) {
                    System.out.println("User Name: " + names[i]);
                }
                loadLanguage(ds.getLocal());
                LoginDialog ld = new LoginDialog(names);
                ld.setVisible(true);
                currentUser = TaskManager.getCurrentUser();
            }
        }
        if (currentUser == null) {
            JOptionPane.showMessageDialog(null, "error when loading user!",
                    "Error", JOptionPane.ERROR_MESSAGE);
            System.out.println("error when loading user!");
            System.exit(0);
        }
        loadLanguage(currentUser.getLocale());

        showMainFrame(currentUser);

        //new ReminderInitiator();
    }

    private void loadLanguage(Locale locale) {
        if (LanguageManager.loadLanguage(locale) == -1) {
            JOptionPane.showMessageDialog(null, "error when loading language",
                    "Startup Error", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    }

    public static void showMainFrame(User user) {
        mainFrame = new MainFrame(ds, user);
        mainFrame.setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Startup app = new Startup();
    }
}
